package com.matt.foundation.sqlOperator;

import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.utils.ModelUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/8
 * *******************************
 */
public class ChildrenCourseOperator {

//    public static final String remoteUrl = "jdbc:mysql://localhost:3306/vega";
//
//    public static final String remoteName = "root";
//
//    public static final String remotePWd = "123456";

        public static final String remoteUrl = "jdbc:mysql://rm-bp151a4j302p495nso.mysql.rds.aliyuncs.com:3306/vega_prd";

    public static final String remoteName = "matt";

    public static final String remotePWd = "HgY5CcO4";

    public static void main(String[] args)throws Exception {
        processChildrenCourseRecord();
    }


    public static void transferType() {
        int num = 0;
        String remoteUrl = "jdbc:mysql://localhost:3306/matt_teach_system";
        String remoteName = "root";
        String remotePWd = "123456";

        String localUrl = "jdbc:mysql://localhost:3306/vega";
        String localName = "root";
        String localPWd = "123456";
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection remoteConn = DriverManager.getConnection(remoteUrl, remoteName, remotePWd);
            PreparedStatement remotePreparedStatement = null;
            ResultSet remoteResultSet = null ;
            String query = "select * from tblsubject  where fldCode not in(10, 11, 12, 13,14) ORDER BY fldCode ";
            remotePreparedStatement = remoteConn.prepareStatement(query);

            remoteResultSet = remotePreparedStatement.executeQuery();
            Connection localConn = DriverManager.getConnection(localUrl, localName, localPWd);
            PreparedStatement localPreparedStatement = null;
            ResultSet localResultSet = null;
            while (remoteResultSet.next()){
                String code = remoteResultSet.getString(1); //value
                String name = remoteResultSet.getString(2); //题目
                Date created = remoteResultSet.getDate(16); //

//                Date createDate = remoteResultSet.getObject(6) == null? null : ( (Date) remoteResultSet.getObject(6)); //传教时间
//                Integer type = remoteResultSet.getInt(7); //4菜谱
//                if(type == 1){
//                    //行为
//                }else if(type == 2){
//                    //游戏
//                }else if(type ==3){
//                    //儿保
//                }else if(type == 100){
//                    //月子餐
//                }

                //8跳过，ApiGroupID
                //9跳过，Tools
//                String img = rs.getString(10);//图片

//                Integer month = rs.getInt(16);//月份
                System.out.println("value :" + code);
                System.out.println("name :" +name);
                System.out.println("created :" +created);
//                System.out.println("img :" + img);
//                System.out.println("month :" + month);


                String insert = "insert into teaching_type_early (id, name, deleted, created) VALUEs (?, ?, ?, ?) ";
//
                localPreparedStatement = localConn.prepareStatement(insert);
//
                localPreparedStatement.setString(1, code);
                localPreparedStatement.setString(2, name);
                localPreparedStatement.setInt(3, 0);
                localPreparedStatement.setDate(4, new java.sql.Date(created.getTime()));
//                localPreparedStatement.setDate(6, new java.sql.Date(createDate.getTime()));
                localPreparedStatement.execute();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }




    public static void processChildrenCourseRecord() throws Exception{

            Class.forName("com.mysql.jdbc.Driver");

            Connection remoteConn = DriverManager.getConnection(remoteUrl, remoteName, remotePWd);
            PreparedStatement remotePreparedStatement = null;
            ResultSet remoteResultSet = null ;
            String query = "select  created, childrenId from children_course_item  where remark like '%系统%' ";
            remotePreparedStatement = remoteConn.prepareStatement(query);
            remoteResultSet = remotePreparedStatement.executeQuery();
            while (remoteResultSet.next()){
                Date created = remoteResultSet.getDate(1); //
                String childrenId = remoteResultSet.getString(2); //value

                //8跳过，ApiGroupID
                //9跳过，Tools
//                String img = rs.getString(10);//图片

//                Integer month = rs.getInt(16);//月份
                System.out.println("childrenId :" + childrenId);
                System.out.println("created :" +created);
                System.out.println(DateUtil.millisToDateTime(DateUtil.plusDays(created.getTime(), 1L), "yyyy-MM-dd"));

                String insert = "insert into children_course_change_record (id, childrenId, created, delta, nowCourseSize, operatorId,  remark) VALUES (?, ?, ?, ?, ?, ?, ?) ";
                remotePreparedStatement = remoteConn.prepareStatement(insert);
                remotePreparedStatement.setString(1, ModelUtil.getCleanUUID());
                remotePreparedStatement.setString(2, childrenId);
                remotePreparedStatement.setDate(3, new java.sql.Date(DateUtil.plusDays(created.getTime(), 1L)));
                remotePreparedStatement.setInt(4, -1);
                remotePreparedStatement.setInt(5, 0);
                remotePreparedStatement.setString(6, null);
                remotePreparedStatement.setString(7, "系统逾期处理");
                remotePreparedStatement.execute();
            }


    }
}
